import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Set;

public class demo02 {
    public static void test() throws InterruptedException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("孤独摇滚");
//        driver.findElement(By.cssSelector("#chat-submit-button")).click();
        System.out.println("网页跳转前的title:"+driver.getTitle());
        System.out.println("网页跳转前的url:"+driver.getCurrentUrl());
        String origin = driver.getWindowHandle();
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        Set<String> windowHandles = driver.getWindowHandles();
        for (String window:windowHandles){
            if (!origin.equals(window)){
                driver.switchTo().window(window);
                break;
            }
        }
        driver.findElement(By.cssSelector("#app > div > div.scroll-container_3GMO1.cos-flex.cos-flex-1.scroll-wrapper_BN2t5 > div > div.cos-flex.cos-flex-col.index-wrapper_B1TLY > div.tools-wrap_1mLZ7 > div.cos-swiper > div.cos-swiper-content > div.cos-swiper-list.cos-swiper-transition-none > div:nth-child(1) > a > img")).click();
        System.out.println("网页跳转前的title:"+driver.getTitle());
        System.out.println("网页跳转前的url:"+driver.getCurrentUrl());
        Thread.sleep(3000);
        driver.quit();
    }

    public static void test02() throws InterruptedException, IOException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
//        driver.manage().window().maximize();
//        Thread.sleep(3000);
//        driver.manage().window().minimize();
//        Thread.sleep(3000);
//        driver.manage().window().fullscreen();
//        Thread.sleep(3000);
//        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//        FileUtils.copyFile(srcFile,new File("test.png"));
        getScreenshotAs(driver);
        driver.quit();
    }

    // ./src/test/images/dirTime/fileTime.png
    public static void getScreenshotAs(WebDriver driver) throws IOException {
        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2=new SimpleDateFormat("HH-mm-ss-SS");

        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime=sim2.format(System.currentTimeMillis());

        String fileName="./src/test/images/"+dirTime+"/"+fileTime+".png";

        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(srcFile,new File(fileName));


    }


    public static void main(String[] args) throws InterruptedException, IOException {
//        test();
        test02();
    }
}
